package com.wt.chat.schedule.processor;

import com.wt.chat.cache.RedisCache;
import com.wt.chat.entity.Dialog;
import com.wt.chat.entity.Message;
import com.wt.chat.model.KefuModel;
import com.wt.chat.service.MessageService;
import com.wt.chat.utils.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @description
 * @author: wangtao
 * @date:15:25 2019/11/19
 * @email:taow02@jumei.com
 */
@Service
public class WelcomeProcessor implements DialogAssignProcessor {

	@Autowired
	private RedisCache redisCache;

	@Autowired
	private MessageService messageService;

	@Override
	public boolean canProcess(ProcessParameter parameter) {
		return true;
	}

	@Override
	public void process(ProcessParameter parameter) {
		Dialog dialog = parameter.getDialog();
		Integer kefuId = dialog.getKefuId();
		Integer userId = dialog.getUserId();
		Integer dialogId = dialog.getId();
		Message message = getMessage(parameter);
		//发送欢迎语
		messageService.sendMessage(message);
		//通知客服和用户有新消息
		redisCache.rightPush(Keys.getNewMessageDialogsKey(Keys.TAG.KEFU , kefuId + ""), dialogId);
		redisCache.rightPush(Keys.getNewMessageDialogsKey(Keys.TAG.USER , userId + ""), dialogId);
	}

	private Message getMessage(ProcessParameter parameter) {
		Message message = new Message();
		message.setCreateTime(new Date());
		message.setDialogId(parameter.getDialog().getId());
		message.setSender(Message.SenderType.KEFU.getValue());
		message.setType(Message.MsgType.TEXT.getValue());
		message.setContent("你好！这个是系统欢迎语！");
		return message;
	}
}
